package com.lixin.smartclassroom.ui.home.tab5.activity

import android.content.Intent
import android.util.Log
import com.drake.net.utils.scopeNetLife
import com.google.zxing.integration.android.IntentIntegrator
import com.journeyapps.barcodescanner.CaptureActivity
import com.lixin.smartclassroom.R
import com.lixin.smartclassroom.api.childclasslist
import com.lixin.smartclassroom.api.classlist
import com.lixin.smartclassroom.api.joinclass
import com.lixin.smartclassroom.api.myjoinclass
import com.lixin.smartclassroom.bean.DataListBean
import com.lixin.smartclassroom.databinding.ActivityMyClassBinding
import com.lixin.smartclassroom.ext.child_id
import com.lixin.smartclassroom.ext.user_role
import com.lixin.smartclassroom.ui.home.adapter.myClassStu
import com.lixin.smartclassroom.ui.home.tab1.activity.ClassDetailAct
import com.lixinkeji.base.BaseActivity
import com.lixinkeji.extension.commonDialog
import com.lixinkeji.extension.gone
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.start
import com.lixinkeji.extension.text
import com.lixinkeji.extension.toast
import com.lixinkeji.extension.visible
import kotlin.math.log

/**
 * Description 我的班级
 * Author: lwc
 * Date:2023/12/8 17:17
 */
class MyClassAct : BaseActivity<ActivityMyClassBinding>() {
    override fun getViewBinding() = ActivityMyClassBinding.inflate(layoutInflater)

    override fun topBarHide() = false
    override fun startPadding() = true


    private lateinit var listBean: ArrayList<DataListBean>
    private var inviteCode = ""

    override fun title(): String {
        return when(user_role){
            1-> "孩子班级"
            else-> "我的班级"
        }
    }

    override fun iniView() {
        listBean = ArrayList()
        scopeNetLife {
            when(user_role){
                1->listBean.addAll(childclasslist(1, 1000,child_id)!!)
                2->listBean.addAll(myjoinclass(1, 1000,"1")!!)
                3->listBean.addAll(myjoinclass(1, 1000,"2")!!)

            }

            binding.recycler.myClassStu().apply {
                R.id.tvInvite.onClick {
                    start(InviteAct::class.java){
                        putExtra("classid",(models?.get(modelPosition) as DataListBean).id)
                    }
                }
                R.id.rl.onClick {
                    start(ClassDetailAct::class.java){
                        putExtra("classid",listBean[modelPosition].id)
                    }
                }
            }.models = listBean
            if (listBean.size > 0) {
                binding.recycler.visible()
                binding.llNoData.gone()
                when (user_role) {
                    3 -> {
                        binding.btnJoinOther.visible()
                    }
                }
            } else {
                binding.recycler.gone()
                binding.llNoData.visible()
            }
            binding.scrollView.visible()
        }


    }


    override fun onClick() {
        binding.apply {
            btnJoinOther.onClick {
                recycler.gone()
                btnJoinOther.gone()
                llNoData.visible()
            }
            btnSave.onClick {
                if(etInviteCode.text().isEmpty()){
                    toast("请输入邀请码")
                    return@onClick
                }
                scopeNetLife {
                    val baseBean = joinclass(etInviteCode.text())
                    if(baseBean.result=="0")
                        commonDialog("加入成功",
                            content = "你已成功加入班级名称",
                            cancel = "返回首页",
                            sure = "去班级",
                            onSure = {
                                start(ClassDetailAct::class.java){
                                    putExtra("classid",baseBean.classid)
                                }
                            })
                }

            }
            btnScan.onClick {
                val integrator = IntentIntegrator(this@MyClassAct)
                // 设置要扫描的条码类型，ONE_D_CODE_TYPES：一维码，QR_CODE_TYPES-二维码
                // 设置要扫描的条码类型，ONE_D_CODE_TYPES：一维码，QR_CODE_TYPES-二维码
                integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES)
                integrator.captureActivity = CaptureAct::class.java
                integrator.setPrompt("扫描条码")
                integrator.setOrientationLocked(false)
                integrator.setCameraId(0) // 使用默认的相机

                integrator.setBeepEnabled(false) // 扫到码后播放提示音

                integrator.setBarcodeImageEnabled(true)
                integrator.initiateScan()
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        val result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
        if (result != null) {
            if (result.contents == null) {
                toast("Cancelled")

            } else {
                inviteCode = result.contents
                Log.e("------------", "inviteCode: $inviteCode" )
                start(ScanResultAct::class.java){
                    putExtra("inviteCode",inviteCode)
                }
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data)
        }
    }

}